/**
 * Created by zhangchuanguo on 2016/12/6.
 */


var util = require('../utils/util.js');
var dicCompanyClass = require('../class/dic-company.js');

module.exports = {

    create: function (params, cb) {
        var models = util.models;
        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var dicCompany = new dicCompanyClass();

        var checkResult = dicCompany.checkParams(
            [
                'name'
            ],
            params);
        if (checkResult) {
            cb(null, util.getErrorRes('400', checkResult));
            return;
        } else {
            dicCompany.init(params);
        }

        models.dic_company.create(dicCompany.getParams(), function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    },

    getList: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        var dicCompany = new dicCompanyClass();
        var checkResult = dicCompany.checkParams(['rowId'], params);

        models.dic_company.find().exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            if (checkResult) {
                cb(null, util.getSuccessRes(data));

            } else {
                dicCompany.init(params);
                var rowId=dicCompany.getParam('rowId');
                data.forEach(function (v, i, a) {
                    if(rowId==v.rowId){
                        console.log(v.rowId);
                        v.selected=true;
                    }
                    return a;

                })
                cb(null, util.getSuccessRes(data));
            }

        });
    },
    getAllList: function (params, cb) {
        var models = util.models;

        if (!models) {
            var err = new Error();
            err.status = 500;
            err.message = '数据库连接失败';
            cb(err, null);
            return;
        }

        models.dic_company.find().exec(function (err, data) {
            if (err || !data) {
                var err = new Error();
                err.status = 500;
                err.message = '系统错误';
                cb(err, null);
                return;
            }

            cb(null, util.getSuccessRes(data));
        });
    }
};



